﻿using System;
using System.Collections.Generic;
using System.Linq;
using CSharpPro;

public class UserManager
{
    private List<User> users = new List<User>();

    public bool Register(string username, string password, string confirmPassword)
    {
    
        if (!IsValidUsername(username))
        {
            Console.WriteLine("用户名无效，请确保长度为3-15个字符，只能包含字母、数字、下划线（_）");
            return false;
        }
        if (!IsValidPassword(password, confirmPassword))
        {
            Console.WriteLine("密码无效，请确保长度为6-20个字符，包含至少1个大写字母、1个小写字母和1个数字，且两次输入一致");
            return false;
        }
        if (users.Any(u => u.Username == username))
        {
            Console.WriteLine("用户名已存在，请选择其他用户名");
            return false;
        }
        
        users.Add(new User(username, password));
        Console.WriteLine("注册成功！");
        return true;
    }

    public bool Login(string username, string password)
    {
      
        var user = users.FirstOrDefault(u => u.Username == username);

        if (user == null)
        {
            Console.WriteLine("用户名不存在");
            return false;
        }

        if (user.Password != password)
        {
            Console.WriteLine("密码错误");
            return false;
        }

        Console.WriteLine("登录成功！");
        return true;
    }

    private bool IsValidUsername(string username)
    {
        if (string.IsNullOrWhiteSpace(username) || username.Length < 3 || username.Length > 15)
        {
            return false;
        }

        return username.All(c => char.IsLetterOrDigit(c) || c == '_');
    }

    private bool IsValidPassword(string password, string confirmPassword)
    {
        if (string.IsNullOrWhiteSpace(password) || password.Length < 6 || password.Length > 20)
        {
            return false;
        }

        if (password != confirmPassword)
        {
            return false;
        }

        return password.Any(char.IsUpper) && password.Any(char.IsLower) && password.Any(char.IsDigit);
    }
}